﻿package ui{
	import flash.display.Sprite
	import flash.display.Stage
	import flash.events.Event
	import flash.text.TextFormatAlign
	
	import flash.events.MouseEvent
	import flash.events.KeyboardEvent
	
	import ui.components.*
	import ui.components.scroll.*
	import playerio.*;
	
	public class Chat extends Sprite{
		private var _connection:Connection
		private var _stage:Stage
		private var base:Box 
		private var input:Input;
		private var content:Rows
		private var container:ScrollBox
		private var userlist:Rows 
		private var users:Object = {};
		function Chat(stage:Stage, connection:Connection){
			this._stage = stage
			this._connection = connection
			base = new Box().margin(5,5,5,5).add(
				new Box().fill(0x0,.5,10).margin(5,5,5,5).add(
					new Box().margin(0,0,NaN,0).add(
						new Box().minSize(0,150).fill(0xffffff,1,3).margin(0,0,0,0).add(
							new ScrollBox().margin(2,2,2,2).add(
								userlist = new Rows().spacing(1)																			
							)
						)
					)
				).add(
					new Box().margin(155,0,32,0).add(
						new Box().fill(0xffffff,1,3).margin(0,0,0,0).add(
							container = new ScrollBox().margin(2,2,2,2).add(
								content = new Rows().spacing(1)
							)		
						)
					)
				).add(
					new Box().margin(NaN,0,0,0).add(
						new Box().minSize(0,27).fill(0xffffff,1,3).margin(3,3,3,3).add(
							new Box().margin(0,60,0,0).add(
								new Box().margin(2,0,0,3).fill(0x666666,0,5).border(1,0x444444,1).add(
									input = new Input("").setStyle(false,false)
								)
							)
						).add(
							new Box().margin(0,0,0,NaN).add(
								new Box().minSize(55,0).margin(0,0,0,0).add(
									new TextButton("Send",sendChatText)
								)
							)
						)
					)
				)
			);
			
			addChild(base);
			
			_connection.addMessageHandler("ChatInit", onInit)
			_connection.addMessageHandler("ChatMessage", onMessage)
			_connection.addMessageHandler("ChatJoin", function(m:Message, id:String, name:String){
				addUser(id,name) 
			})
			_connection.addMessageHandler("ChatLeft", function(m:Message, id:String){
				removeUser(id) 
			})
			
			input.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent){
				if(e.keyCode == 13){
					sendChatText();
				}									
			})
			
			
			stage.addChild(this);
			_stage.addEventListener(Event.RESIZE, handleResize)
			handleResize();
		}
		
		
		
		public function sendMessage(message:String){
			if(_connection.connected && message != ""){
				_connection.send("ChatMessage", message)
			}
		}
		
		private function sendChatText():void{
			sendMessage(input.text)
			input.clear();
		}
		
		private function onInit(m:Message, id:String){
			for( var a:int=1;a<m.length;a+=2){
				addUser(m.getString(a),m.getString(a+1))
			}
		}
		
		private function addUser(id:String, username:String){
			if(!users[id]){
				users[id] = new Label(username, 12, TextFormatAlign.LEFT, 0x0, true)
				userlist.addChild(users[id])
				base.reset();
			}
		}

		private function removeUser(id:String){
			if(users[id]){
				userlist.removeChild(users[id])
				delete users[id]
				base.reset();
			}
		}
		
		private function onMessage(m:Message, id:String, message:String){
			if(content.numChildren > 50){
				content.removeChild( content.getChildAt(0) );
			}
			content.addChild(
				new Label(users[id].text + ": " + message, 12, TextFormatAlign.LEFT, 0x0, true)
			)
			base.reset();
			container.scrollY = 100000
			
		}
		
		
		private function handleResize(e:Event = null){
			base.height = _stage.stageHeight
			base.width = 250;
			base.x = _stage.stageWidth - 250
		}
	}	
}